﻿using FlyingEye.Utils;
using System.IO;
using System.Reflection;
using System;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.Modularity;

namespace FlyingEye
{
    [DependsOn(
        typeof(Volo.Abp.Autofac.AbpAutofacModule),
        typeof(FlyingEye.EntityFrameworkCore.MySqlEntityFrameworkCoreModule))]
    public class FlyingEyeDbMigratorModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            Configure<AbpBackgroundJobOptions>(options => options.IsJobExecutionEnabled = false);

            // 设置应用程序启动路径，一定是基于主函数所在的模块作为启动路径。
            var mainAsselbly = Assembly.GetAssembly(typeof(FlyingEyeDbMigratorModule));

            if (mainAsselbly is null)
            {
                throw new System.InvalidOperationException("主模块数程序集为 NULL！");
            }

            var startPath = Path.GetDirectoryName(mainAsselbly.Location);

            if (string.IsNullOrWhiteSpace(startPath) || !Directory.Exists(startPath))
            {
                throw new InvalidOperationException("无效的启动路径！");
            }

            Configure<ApplicationStartInOptions>(options =>
            {
                options.StartPath = startPath;
            });
        }
    }
}
